﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB

Public Class STK_ItemInventory
    Implements ICarfDataEditingChildWindow


#Region "Declarations"
    Dim iDataObject As StockItemType = Nothing
    Dim iDataObjectInventory As InventoryTypeCollection = Nothing
    Dim frmMain As SVCM_Form = Nothing
    Dim MenuCheckStateFromGridHasBeenInitialized As Boolean = False
#End Region

#Region "Overloads/Overrides"


    Public Overloads Sub Show(ByRef DataObject As StockItemType)
        If DataObject IsNot Nothing Then
            Me.frmMain = Me.MdiParent
            Me.iDataObject = DataObject
            Setup_StockGrid()
            UpdateCheckState_FromGridToMenu()
            Refresh_InventoryMethodsList()
            Refresh_ControlsWithDataObject()
            Me.Show()
        Else
            Me.Close()
            Exit Sub
        End If
    End Sub

#End Region

#Region "Events"
    Private Sub STK_StockItemInventory_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.InitializeThisFormAndControlSettings()
    End Sub
    Private Sub STK_StockItemInventory_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub

    'Quantity
    Private Sub Quantity_NumericUpDown_ValueChanged(sender As System.Object, e As System.EventArgs) Handles Quantity_NumericUpDown.ValueChanged
        Refresh_StockItemProperties()
    End Sub
    'AddInventory
    Private Sub AddInventory_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles AddInventory_ImageButton.Click
        If Quantity_NumericUpDown.Value = 0 Then
            Me.frmMain.StatusInfoMsgPut("Quantity is 0 does not make any sense, does it...?")
            Exit Sub
        End If
        If InventoryMethod_StylishComboBox.SelectedItem Is Nothing Then
            Me.frmMain.StatusInfoMsgPut("Inventory method not set...")
            Exit Sub
        End If
        If Remarks_TextBox.Text = "" Then
            Me.frmMain.StatusInfoMsgPut("An explaination for inventory should be provided in remarks...")
            Exit Sub
        End If
        Dim inv As New InventoryType(Guid.NewGuid)

        With inv
            .StockItemId = Me.iDataObject.StockItemId
            .RefType = InventoryMethod_StylishComboBox.SelectedItem
            .RefId = Me.iDataObject.StockItemId
            .InventoryQuantity = Quantity_NumericUpDown.Value
            .InventoryCostPrice = Me.iDataObject.CostPrice
            .InventorySellingPrice = Me.iDataObject.SellingPrice
            .InventoryTaxPercentage = Me.iDataObject.TaxPercentage
            .Remarks = Remarks_TextBox.Text
        End With

        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        stkMan.Update_Inventory(inv)

        Init_EntryControls()
        Refresh_StockItemData()
        Refresh_ControlsWithDataObject()
        Me.OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.NewRecord)

        Me.frmMain.StatusInfoMsgPut("Inventory record has been successfully added")

    End Sub
    'Close 
    Private Sub Close_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Close_ImageButton.Click
        Me.Close()
    End Sub
    'Columns ContextMenuItems
    Private Sub Sequence_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Sequence_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub InventoryMethod_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles InventoryMethod_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub AlteredDate_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles AlteredDate_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub AlteredByUser_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles AlteredByUser_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Quantity_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Quantity_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Remarks_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Remarks_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub CostPrice_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles CostPrice_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub SellingPrice_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles SellingPrice_ToolStripMenuItem.CheckedChanged
        Me.UpdateCheckState_FromMenuToGrid()
    End Sub

#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        If (DataObject IsNot Nothing) _
            AndAlso (TypeOf DataObject Is DataRow) Then

            If DataObject.Table.Columns.Contains("StockItemId") Then
                Dim stockItemId1 As Guid = DataObject.Item("StockItemId")
                If stockItemId1.Equals(Me.iDataObject.StockItemId) Then
                    Return Me
                End If
            End If
        ElseIf (DataObject IsNot Nothing) _
            AndAlso (TypeOf DataObject Is StockItemType) Then
            If Me.iDataObject.StockItemId.Equals(DataObject.StockItemId) Then
                Return Me
            End If
        End If

        Return Nothing
    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls
        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, RelatedControlDataCategoriesEnum.StockItemInventory, DataItem, ActionOnDataItem)
    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls
        'no refresh required
    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        Refresh_StockItemProperties()
        Refresh_InventoryData()
        Refresh_InventoryGrid()
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls

    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Refresh_InventoryMethodsList()
        InventoryMethod_StylishComboBox.Items.Clear()
        Dim vals() As STK_InventoryMethodsEnum = [Enum].GetValues(GetType(STK_InventoryMethodsEnum))
        For Each Val As STK_InventoryMethodsEnum In vals
            If Val = STK_InventoryMethodsEnum.File_Import _
                OrElse Val = STK_InventoryMethodsEnum.GRN_Entry _
                OrElse Val = STK_InventoryMethodsEnum.Manual_Stock_Entry Then Continue For
            InventoryMethod_StylishComboBox.Items.Add(Val)
        Next
    End Sub
    Private Sub Setup_StockGrid()

        If Inventory_DataGridView.Columns.Count < 1 Then
            Inventory_DataGridView.AutoGenerateColumns = False
            Inventory_DataGridView.Columns.Clear()

            'Sequence
            Dim Sequence_Column As New DataGridViewTextBoxColumn
            With Sequence_Column
                .DataPropertyName = "Sequence"
                .Name = "Sequence_Column"
                .HeaderText = "Sequence"
                .Visible = False
            End With
            Inventory_DataGridView.Columns.Add(Sequence_Column)

            'InventoryMethod(RefType)
            Dim InventoryMethod_Column As New DataGridViewTextBoxColumn
            With InventoryMethod_Column
                .DataPropertyName = "RefType"
                .Name = "InventoryMethod_Column"
                .HeaderText = "InventoryMethod"
                .Visible = True
            End With
            Inventory_DataGridView.Columns.Add(InventoryMethod_Column)

            'AlteredDate
            Dim AlteredDate_Column As New DataGridViewTextBoxColumn
            With AlteredDate_Column
                .DataPropertyName = "AlteredDate"
                .Name = "AlteredDate_Column"
                .HeaderText = "AlteredDate"
                .DefaultCellStyle.Format = "dd-MMM-yy HH:mm"
                .Visible = True
            End With
            Inventory_DataGridView.Columns.Add(AlteredDate_Column)

            'AlteredByUser
            Dim AlteredByUser_Column As New DataGridViewTextBoxColumn
            With AlteredByUser_Column
                .DataPropertyName = "AlteredByUser"
                .Name = "AlteredByUser_Column"
                .HeaderText = "AlteredByUser"
                .Visible = True
            End With
            Inventory_DataGridView.Columns.Add(AlteredByUser_Column)

            'Quantity
            Dim Quantity_Column As New DataGridViewTextBoxColumn
            With Quantity_Column
                .DataPropertyName = "Quantity"
                .Name = "Quantity_Column"
                .HeaderText = "Quantity"
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .Visible = True
            End With
            Inventory_DataGridView.Columns.Add(Quantity_Column)

            'Remarks
            Dim Remarks_Column As New DataGridViewTextBoxColumn
            With Remarks_Column
                .DataPropertyName = "Remarks"
                .Name = "Remarks_Column"
                .HeaderText = "Remarks"
                .Visible = True
            End With
            Inventory_DataGridView.Columns.Add(Remarks_Column)

            'CostPrice
            Dim CostPrice_Column = New DataGridViewTextBoxColumn
            With CostPrice_Column
                .DataPropertyName = "CostPrice"
                .Name = "CostPrice_Column"
                .HeaderText = "CostPrice"
                .DefaultCellStyle.Format = "N2"
                .Visible = False
            End With
            Inventory_DataGridView.Columns.Add(CostPrice_Column)

            'SellingPrice
            Dim SellingPrice_Column = New DataGridViewTextBoxColumn
            With SellingPrice_Column
                .DataPropertyName = "SellingPrice"
                .Name = "SellingPrice_Column"
                .HeaderText = "SellingPrice"
                .DefaultCellStyle.Format = "N2"
                .Visible = False
            End With
            Inventory_DataGridView.Columns.Add(SellingPrice_Column)

        End If

    End Sub
    Private Sub Refresh_StockItemData()
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Me.iDataObject = stkMan.Get_StockItem_FromStockItemId(Me.iDataObject.StockItemId)
    End Sub
    Private Sub Refresh_StockItemProperties()

        ItemCode_RoundCornerLabel.Text = Me.iDataObject.ItemCode
        Description_RoundCornerLabel.Text = Me.iDataObject.Description
        CurrentQty_RoundCornerLabel.Text = Format(Me.iDataObject.Quantity, "N2")
        NewQty_RoundCornerLabel.Text = Format(Me.iDataObject.Quantity + Quantity_NumericUpDown.Value, "N2")

    End Sub
    Private Sub Refresh_InventoryData()

        Dim stkman As New StkManager(AppSettings.LoggedUser)
        Me.iDataObjectInventory = stkman.Get_Inventory_ForStockItemId_Col(Me.iDataObject.StockItemId)

    End Sub
    Private Sub Refresh_InventoryGrid()


        'Inventory_DataGridView.AutoGenerateColumns = False
        'Inventory_DataGridView.DataSource = inventory
        'Method,Date,ByUser,Quantity,Remarks,CostPrice,SellingPrice
        'Inventory_DataGridView.Rows.Clear()
        Inventory_DataGridView.AutoGenerateColumns = False
        'Me.iDataObjectInventory.Sort(InventoryTypeFields.AlteredDate, SqlClient.SortOrder.Descending)
        Inventory_DataGridView.DataSource = Me.iDataObjectInventory

        'If Me.iDataObjectInventory.Count > 0 Then
        '    For i As Integer = 0 To Me.iDataObjectInventory.Count - 1
        '        Dim inv As InventoryType = Me.iDataObjectInventory.Item(i)
        '        If inv.RefType = STK_InventoryMethodsEnum.Manual_Stock_Entry _
        '            OrElse inv.RefType = STK_InventoryMethodsEnum.CSV_Stock_Import Then
        '            Dim row() As Object = {inv.RefType.ToString.Replace("_", " "), _
        '                                   Format(inv.AlteredDate, "dd-MMM-yy HH:mm"), _
        '                                   inv.AlteredByUser, _
        '                                   Format(inv.Quantity, "N2"), _
        '                                   inv.Remarks, _
        '                                   Format(inv.CostPrice, "N2"), _
        '                                   Format(inv.SellingPrice, "N2")}
        '            Inventory_DataGridView.Rows.Add(row)
        '        Else
        '            Dim row() As Object = {inv.RefType.ToString.Replace("_", " "), _
        '                                   Format(inv.AlteredDate, "dd-MMM-yy HH:mm"), _
        '                                   inv.AlteredByUser, _
        '                                   Format(inv.Quantity, "N2"), _
        '                                   inv.Remarks, _
        '                                   "", _
        '                                   ""}
        '            Inventory_DataGridView.Rows.Add(row)
        '        End If
        '    Next
        'End If

    End Sub
    Private Sub Init_EntryControls()
        Quantity_NumericUpDown.Value = 0
        Remarks_TextBox.Text = ""
    End Sub
    Private Sub UpdateCheckState_FromGridToMenu()
        'Sequence
        Sequence_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("Sequence_Column").Visible
        'InventoryMethod
        InventoryMethod_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("InventoryMethod_Column").Visible
        'AlteredDate
        AlteredDate_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("AlteredDate_Column").Visible
        'AlteredByUser
        AlteredByUser_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("AlteredByUser_Column").Visible
        'Quantity
        Quantity_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("Quantity_Column").Visible
        'Remarks
        Remarks_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("Remarks_Column").Visible
        'CostPrice
        CostPrice_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("CostPrice_Column").Visible
        'SellingPrice
        SellingPrice_ToolStripMenuItem.Checked = Me.Inventory_DataGridView.Columns("SellingPrice_Column").Visible

        Me.MenuCheckStateFromGridHasBeenInitialized = True
    End Sub
    Private Sub UpdateCheckState_FromMenuToGrid()
        If MenuCheckStateFromGridHasBeenInitialized Then
            Me.Inventory_DataGridView.Columns("Sequence_Column").Visible = Sequence_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("InventoryMethod_Column").Visible = InventoryMethod_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("AlteredDate_Column").Visible = AlteredDate_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("AlteredByUser_Column").Visible = AlteredByUser_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("Quantity_Column").Visible = Quantity_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("Remarks_Column").Visible = Remarks_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("CostPrice_Column").Visible = CostPrice_ToolStripMenuItem.Checked
            Me.Inventory_DataGridView.Columns("SellingPrice_Column").Visible = SellingPrice_ToolStripMenuItem.Checked
        End If
    End Sub
#End Region






End Class
